var pageBean = null;
var dataList = {};
var provinces = {};
var cities = {};
var spaces = {};
var clues = {};
var productTypes = {};
var processPersons = {};
var sourceOfChannels = {};
var currentData = null;

$(document).ready(function(e) {
	// 切换客户类型输入信息
	$("#formClientType").change(clientTypeChange);

	$("#btnCancle").click(cancle);
	$("#btnClean").click(clean);

	initialPlaces();
	initialSpace();
	initialSourceOfChannel();
	initialProductType();
	search(1);
	initialSelect();
	initialProcessPerson();
	$("SELECT[name='placeProvinceSelect']").change(provinceChange);
	$("SELECT[name='provinceId']").change(provinceIdChange);
	$("SELECT[name='placeCitySelect']").change(cityChange);
	$("SELECT[name='cityId']").change(cityIdChange);

	// 新增保存
	$("#btn-save").click(function() {
		// contact=%E6%B5%8B%E8%AF%95&contact_number=13525931134&space_id=1&visit_time=2018-04-06
		$space_id = parseInt($("#y_space").val());
		if (!$space_id || $space_id == 0) {
			alert("请选择预约空间");
			return false;
		}
		$contact = $("#y_username").val();
		if (!$contact) {
			alert("请填写预约人");
			return false;
		}
		$contact_number = $("#y_mobile").val();
		if (!$contact_number) {
			alert("请填写预约电话");
			return false;
		}
		$visit_time = $("#y_time").val();
		if (!$visit_time) {
			alert("请选择预约参观时间");
			return false;
		}
		$formSourceChannel = $("#y_formSourceChannel").val();
		if (!$formSourceChannel || $formSourceChannel == 0) {
			alert("请选择客户来源");
			return false;
		}

		var requestJson = {
			"space_id" : $space_id,
			"contact" : $contact,
			"contact_number" : $contact_number,
			"visit_time" : $visit_time,
			"formSourceChannel" : $formSourceChannel
		};

		$.ajax({
			url : '/euc/_space/space_reserve',
			data : requestJson,
			beforeSend : function(request) {
				var accessToken = localStorage.getItem('accessToken');
				request.setRequestHeader("accessToken", accessToken);
			},
			dataType : 'JSON',
			async : true,
			contentType : 'application/x-www-form-urlencoded',
			type : 'POST',
			success : function(result, textStatus, response) {
				if (result.status == 0) {
					$('#addModal').modal('hide');
					alert("新增成功！");
					window.location.reload();

				} else {
					$('#addModal').modal('hide')
					alert(result.message);
				}
			},
			error : function() {

			}
		});

	});
});

function initialProcessPerson() {

	var requestJson = {
		functionId : '1120'
	};
	$.ajax({
		url : '/euc/user/functionusers',
		data : JSON.stringify(requestJson),
		beforeSend : function(request) {
			var accessToken = localStorage.getItem('accessToken');
			request.setRequestHeader("accessToken", accessToken);
		},
		dataType : 'JSON',
		async : true,
		contentType : 'application/json;charset=utf-8',
		type : 'POST',
		success : function(result, textStatus, response) {
			var processPersonSelect = $("SELECT[name='processPerson']");
			$.each(result, function(i, data) {
				var style = '';
				if (data.enable == 'false' || data.enable == false) {
					style = 'style="color:red;"';
				}
				$(
						'<option ' + style + ' value="' + data.id + '">'
								+ data.name + '</option>').appendTo(
						$(processPersonSelect));
				processPersons[data.id] = data;
			});
			$(processPersonSelect).val(0).trigger('change');
		},
		error : function() {
			console.log("/euc/user/functionusers error");
		}
	});

}

function clean() {
	var provinceSelect = $("SELECT[name='placeProvinceSelect']");
	$(provinceSelect).val(0).trigger('change');

	var citiesSelect = $("SELECT[name='placeCitySelect']");
	$.each(cities, function(key, city) {
		$('<option value="' + city.cityId + '">' + city.cityName + '</option>')
				.appendTo($(citiesSelect));
	});
	$(citiesSelect).val(0).trigger('change');

	var spaceSelect = $("SELECT[name='spaceSelect']");
	$.each(spaces, function(key, space) {
		$('<option value="' + space.id + '">' + space.spaceName + '</option>')
				.appendTo($(spaceSelect));
	});
	$(spaceSelect).val(0).trigger('change');
	$('input[name="visitTimeStart"]').val('');
	$('input[name="visitTimeEnd"]').val('');
}

function initialSelect() {

	var provinceSelect = $("SELECT[name='placeProvinceSelect']");
	var provinceIdSelect = $("SELECT[name='provinceId']");
	$.each(provinces, function(key, province) {// key provinceId,val province
												// json object
		$('<option value="' + key + '">' + province.provinceName + '</option>')
				.appendTo($(provinceSelect));
		$('<option value="' + key + '">' + province.provinceName + '</option>')
				.appendTo($(provinceIdSelect));
	});

	var citiesSelect = $("SELECT[name='placeCitySelect']");
	var cityIdSelect = $("SELECT[name='cityId']");
	$.each(cities, function(key, city) {
		$('<option value="' + key + '">' + city.cityName + '</option>')
				.appendTo($(citiesSelect));
		$('<option value="' + key + '">' + city.cityName + '</option>')
				.appendTo($(cityIdSelect));
	});

	var spaceSelect = $("SELECT[name='spaceSelect']");
	var spaceIdSelect = $("SELECT[name='spaceId']");
	$.each(spaces, function(key, space) {
		$('<option value="' + key + '">' + space.spaceName + '</option>')
				.appendTo($(spaceSelect));
		$('<option value="' + key + '">' + space.spaceName + '</option>')
				.appendTo($(spaceIdSelect));
	});
	$(spaceSelect).val(0).trigger('change');
	$(spaceSelect).val(0).trigger('change');

	var sourceChannelIdSelect = $("SELECT[name='sourceChannelId']");
	$.each(sourceOfChannels, function(key, sourceOfChannel) {
		$(
				'<option value="' + key + '">'
						+ sourceOfChannel.sourceOfChannelName + '</option>')
				.appendTo($(sourceChannelIdSelect));
	});
	$(sourceChannelIdSelect).val(0).trigger('change');

	var productTypeIdSelect = $("SELECT[name='productTypeId']");
	$.each(productTypes, function(key, productType) {
		$(
				'<option value="' + key + '">' + productType.productTypeName
						+ '</option>').appendTo($(productTypeIdSelect));
	});
	$(productTypeIdSelect).val(0).trigger('change');
}

function cityChange() {
	var cityIdSelected = $(this).children('option:selected').val();
	var spaceSelect = $("SELECT[name='spaceSelect']");
	$(spaceSelect).empty();
	$('<option value="0" selected>请选择</option>').appendTo($(spaceSelect));
	if (cityIdSelected == '0') {
		$.each(spaces, function(key, space) {
			$(
					'<option value="' + space.id + '">' + space.spaceName
							+ '</option>').appendTo($(spaceSelect));
		});
	} else {
		$.each(spaces, function(key, space) {
			if (cityIdSelected == space.city.cityId) {
				$(
						'<option value="' + space.id + '">' + space.spaceName
								+ '</option>').appendTo($(spaceSelect));
			}
		});
	}
	$(spaceSelect).val(0).trigger('change');
}

function cityIdChange() {
	var cityIdSelected = $(this).children('option:selected').val();
	var spaceIdSelect = $("SELECT[name='spaceId']");
	$(spaceIdSelect).empty();
	$('<option value="0" selected>请选择</option>').appendTo($(spaceIdSelect));
	if (cityIdSelected == '0') {
		$.each(spaces, function(key, space) {
			$(
					'<option value="' + space.id + '">' + space.spaceName
							+ '</option>').appendTo($(spaceIdSelect));
		});
	} else {
		$.each(spaces, function(key, space) {
			if (cityIdSelected == space.city.cityId) {
				$(
						'<option value="' + space.id + '">' + space.spaceName
								+ '</option>').appendTo($(spaceIdSelect));
			}
		});
	}
	$(spaceIdSelect).val(0).trigger('change');
}

function provinceChange() {
	var provinceIdSelected = $(this).children('option:selected').val();
	var citiesSelect = $("SELECT[name='placeCitySelect']");
	$(citiesSelect).empty();
	$('<option value="0" selected>请选择</option>').appendTo($(citiesSelect));
	if (provinceIdSelected != '0') {
		var province = provinces[provinceIdSelected];
		$.each(province.cities, function(index, data) {
			$(
					'<option value="' + data.cityId + '">' + data.cityName
							+ '</option>').appendTo($(citiesSelect));
		});
	} else {
		$.each(cities, function(key, city) {
			$(
					'<option value="' + city.cityId + '">' + city.cityName
							+ '</option>').appendTo($(citiesSelect));
		});
	}
	$(citiesSelect).val(0).trigger('change');
}

function provinceIdChange() {
	var provinceIdSelected = $(this).children('option:selected').val();
	var cityIdSelect = $("SELECT[name='cityId']");
	$(cityIdSelect).empty();
	$('<option value="0" selected>请选择</option>').appendTo($(cityIdSelect));
	if (provinceIdSelected != '0') {
		var province = provinces[provinceIdSelected];
		$.each(province.cities, function(index, data) {
			$(
					'<option value="' + data.cityId + '">' + data.cityName
							+ '</option>').appendTo($(cityIdSelect));
		});
	} else {
		$.each(cities, function(key, city) {
			$(
					'<option value="' + city.cityId + '">' + city.cityName
							+ '</option>').appendTo($(cityIdSelect));
		});
	}
	$(cityIdSelect).val(0).trigger('change');
}

function initialPlaces() {
	try {
		var provinceString = localStorage.getItem('province');
		var cityString = localStorage.getItem('city');
		provinces = $.parseJSON(provinceString);
		cities = $.parseJSON(cityString);
	} catch (e) {
		console.log(e.messge);
	}
}

function initialSpace() {
	try {
		var spaceString = localStorage.getItem('space');
		spaces = $.parseJSON(spaceString);
	} catch (e) {
		console.log(e.messge);
	}
	$.ajax({
		url : '/euc/system/space',
		data : '',
		beforeSend : function(request) {
			var accessToken = localStorage.getItem('accessToken');
			request.setRequestHeader("accessToken", accessToken);
		},
		dataType : 'JSON',
		async : true,
		contentType : 'application/json;charset=utf-8',
		type : 'POST',
		success : function(result, textStatus, response) {
			try {
				$.each(result, function(index, data) {
					spaces[data.id] = data;
				});
				var spaceJson = JSON.stringify(spaces);
				localStorage.setItem('space', spaceJson);
			} catch (e) {
				/* alert(e.message); */
			}
		},
		error : function() {
			/* alert("error"); */
		}
	});
}

function initialSourceOfChannel() {
	try {
		var sourceOfChannelString = localStorage.getItem('sourceOfChannel');
		sourceOfChannels = $.parseJSON(sourceOfChannelString);
	} catch (e) {
		console.log(e.messge);
	}
	$.ajax({
		url : '/euc/system/sourceofchannel',
		data : '',
		beforeSend : function(request) {
			var accessToken = localStorage.getItem('accessToken');
			request.setRequestHeader("accessToken", accessToken);
		},
		dataType : 'JSON',
		async : true,
		contentType : 'application/json;charset=utf-8',
		type : 'POST',
		success : function(result, textStatus, response) {
			try {
				$.each(result, function(index, data) {
					sourceOfChannels[data.id] = data;
				});
				var sourceOfChannelJson = JSON.stringify(sourceOfChannels);
				localStorage.setItem('sourceOfChannel', sourceOfChannelJson);
			} catch (e) {
				console.log(e.messge);
			}
		},
		error : function() {
			console.log('/euc/system/sourceofchannel error');
		}
	});
}

function initialProductType() {
	try {
		var productTypeString = localStorage.getItem('productType');
		productTypes = $.parseJSON(productTypeString);
	} catch (e) {
		console.log(e.messge);
	}
	$.ajax({
		url : '/euc/system/producttype',
		data : '',
		beforeSend : function(request) {
			var accessToken = localStorage.getItem('accessToken');
			request.setRequestHeader("accessToken", accessToken);
		},
		dataType : 'JSON',
		async : true,
		contentType : 'application/json;charset=utf-8',
		type : 'POST',
		success : function(result, textStatus, response) {
			try {
				$.each(result, function(index, data) {
					productTypes[data.id] = data;
				});
				var productTypeJson = JSON.stringify(productTypes);
				localStorage.setItem('productType', productTypeJson);
			} catch (e) {
				/* alert(e.message); */
			}
		},
		error : function() {
			/* alert("error"); */
		}
	});
}

function clientTypeChange() {// 切换客户类型输入信息
	var clientType = $(this).children('option:selected').val();
	$("[clientType]").hide();
	$("[clientType='" + clientType + "']").show();
}

function getClueJson(clue) {
	$("[formParameter='clue']").each(
			function(index, element) {
				try {
					var required = $(element).prop("required");
					var name = $(element).attr("name");
					var val = $(element).val();
					if ((required == '1' || required == true)
							&& ($.trim(val) == '' || $.trim(val) == '0')) {
						var message = $(element).attr("message");
						alert(message);
						clue = false;
						return false;
					}
					clue[name] = val;
				} catch (e) {
					console.log(e.messge);
				}
			});
	if (clue != false) {
		try {
			clue['processPerson'] = processPersons[$(
					"SELECT[name='processPerson']").val()];
		} catch (e) {
			console.log(e.messge);
		}

		var client = clue['client'];
		try {
			if (client == null) {
				client = {};
			}
			$("[formParameter='client']").each(function(index, element) {
				var name = $(element).attr("name");
				client[name] = $.trim($(element).val());
			});
			clue['client'] = client;
		} catch (e) {
			console.log(e.messge);
		}

	}
	return clue;
}

function update(id) {
	currentData = dataList[id];
	if (currentData == null) {
		return false;
	}
	currentData = getClueJson(currentData);
	console.log(JSON.stringify(currentData));
	if (currentData == false) {
		return false;
	}
	$.ajax({
		url : '/euc/clue/update',
		data : JSON.stringify(currentData),
		beforeSend : function(request) {
			var accessToken = localStorage.getItem('accessToken');
			request.setRequestHeader("accessToken", accessToken);
		},
		dataType : 'JSON',
		async : true,
		contentType : 'application/json;charset=utf-8',
		type : 'POST',
		success : function(datas, textStatus, response) {
			if (datas.status == 0) {
				$("DIV[name='dataList']").show();
				$("DIV[name='dataForm']").hide();
				currentData = null;
				search(pageBean.currentPage);
			} else {
				alert(datas.message);
			}
		},
		error : function() {
			/* alert("error"); */
		}
	});
}

function cancle() {
	$("DIV[name='dataList']").show();
	$("DIV[name='dataForm']").hide();
}

function clearForm() {

	var provinceIdSelect = $("SELECT[name='provinceId']");
	$(provinceIdSelect).val(0).trigger('change');

	var cityIdSelect = $("SELECT[name='cityId']");
	$(cityIdSelect).empty();
	$('<option value="0" selected>请选择</option>').appendTo($(cityIdSelect));
	$.each(cities, function(key, city) {
		$('<option value="' + city.cityId + '">' + city.cityName + '</option>')
				.appendTo($(cityIdSelect));
	});
	$(cityIdSelect).val(0).trigger('change');

	var spaceIdSelect = $("SELECT[name='spaceId']");
	$(spaceIdSelect).empty();
	$('<option value="0" selected>请选择</option>').appendTo($(spaceIdSelect));
	$.each(spaces, function(key, space) {
		$('<option value="' + space.id + '">' + space.spaceName + '</option>')
				.appendTo($(spaceIdSelect));
	});
	$(spaceIdSelect).val(0).trigger('change');

	$("[formParameter]").each(function(index, element) {
		try {
			var tagName = $(element).prop("tagName");
			if (tagName == 'SELECT') {
				var firstVal = $(element).find('option').eq(0).val();
				$(element).val(firstVal).trigger('change');
			} else {
				$(element).val('');
			}
		} catch (e) {
			console.log(e.messge);
		}
	});
}

function setForm(clue) {
	$("[formParameter='clue']").each(function(index, element) {
		try {
			var tagName = $(element).prop("tagName");
			var name = $(element).attr("name");
			if (tagName == 'SELECT') {
				$(element).val(clue[name]).trigger('change');
			} else {
				$(element).val(clue[name]);
			}
		} catch (e) {
			console.log(e.messge);
		}
	});
	try {
		$("SELECT[name='provinceId']").val(clue['provinceId'])
				.trigger('change');
		$("SELECT[name='cityId']").val(clue['cityId']).trigger('change');
		$("SELECT[name='spaceId']").val(clue['spaceId']).trigger('change');
		$("SELECT[name='processPerson']").val(clue['processPerson'].id)
				.trigger('change');
	} catch (e) {
		console.log(e.messge);
	}

	var client = clue['client'];
	$("[formParameter='client']").each(function(index, element) {
		try {
			var tagName = $(element).prop("tagName");
			var name = $(element).attr("name");
			if (tagName == 'SELECT') {
				$(element).val(client[name]).trigger('change');
			} else {
				$(element).val(client[name]);
			}
		} catch (e) {
			console.log(e.messge);
		}
	});

	try {
		var clientType = $("[formParameter='client'][name='clientType']").val();
		if (clientType == '个人') {
			$("[formParameter='client'][name='memberName']").val(
					$.trim(clue['contact']));
		} else if (clientType == '企业') {
			$("[formParameter='client'][name='companyName']").val(
					$.trim(clue['contact']));
		}
	} catch (e) {
		console.log(e.message);
	}
}

function detail(id) {
	currentData = dataList[id];
	console.log(JSON.stringify(currentData));
	clearForm();
	setForm(currentData);
	$("#operator").html("编辑");
	$("DIV[name='dataList']").hide();
	$("DIV[name='dataForm']").show();
	$("#btnSave").unbind();
	$("#btnSave").click(function() {
		update(id);
	});
}

function deleteData(id) {
	if (confirm("确定删除该线索?")) {
		var requestJson = {
			id : id
		};
		$.ajax({
			url : '/euc/clue/delete',
			data : JSON.stringify(requestJson),
			beforeSend : function(request) {
				var accessToken = localStorage.getItem('accessToken');
				request.setRequestHeader("accessToken", accessToken);
			},
			dataType : 'JSON',
			async : true,
			contentType : 'application/json;charset=utf-8',
			type : 'POST',
			success : function(result, textStatus, response) {
				$('tr#t'+id).remove();
				var cur = $('.paginItem.current').find("a").html();
				cur = parseInt(cur);
				search(cur);
				alert(result.message);
			},
			error : function() {
				alert("error");
			}
		});
	}
}

function initailPageNumber(totalPage, currentPage, pageLength) {
	try {
		var midNumber = Math.ceil((pageLength - 2) / 2);
		var paginList = $(".paginList");
		$(paginList).empty();
		var prev = '<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>';
		var next = '<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>';
		if (totalPage <= pageLength - 2) {
			$(prev).appendTo($(".paginList"));
			for (var i = 1; i < totalPage + 1; i++) {
				var pagenItem = '<li class="paginItem"><a href="javascript:;">'
						+ i + '</a></li>';
				if (i == currentPage) {
					pagenItem = '<li class="paginItem current"><a href="javascript:;">'
							+ i + '</a></li>';
				}
				$(pagenItem).appendTo($(".paginList"));
			}
			$(next).appendTo($(".paginList"));
		} else {
			if (currentPage <= midNumber) {
				$(prev).appendTo($(".paginList"));
				for (var i = 1; i <= pageLength - 4; i++) {
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'
							+ i + '</a></li>';
					if (i == currentPage) {
						pagenItem = '<li class="paginItem current"><a href="javascript:;">'
								+ i + '</a></li>';
					}
					$(pagenItem).appendTo($(".paginList"));
				}
				var pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
				$(pagenItem).appendTo($(".paginList"));
				var pagenItem = '<li class="paginItem"><a href="javascript:;">'
						+ totalPage + '</a></li>';
				$(pagenItem).appendTo($(".paginList"));
				$(next).appendTo($(".paginList"));
			}
			if (totalPage - currentPage < midNumber) {

				var index = totalPage;
				for (var i = 1; i <= pageLength - 4; i++) {
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'
							+ index + '</a></li>';
					if (index == currentPage) {
						pagenItem = '<li class="paginItem current"><a href="javascript:;">'
								+ index + '</a></li>';
					}
					$(pagenItem).prependTo($(".paginList"));
					index--;
				}
				var pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
				$(pagenItem).prependTo($(".paginList"));
				var pagenItem = '<li class="paginItem"><a href="javascript:;">'
						+ 1 + '</a></li>';
				$(pagenItem).prependTo($(".paginList"));
				$(prev).prependTo($(".paginList"));

				$(next).appendTo($(".paginList"));
			} else if (totalPage - currentPage >= midNumber
					&& currentPage > midNumber) {

				pagenItem = '<li class="paginItem current"><a href="javascript:;">'
						+ currentPage + '</a></li>';
				$(pagenItem).prependTo($(".paginList"));
				var count = midNumber - 3;
				for (var i = 1; i <= count; i++) {
					pagenItem = '<li class="paginItem"><a href="javascript:;">'
							+ (currentPage - i) + '</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					pagenItem = '<li class="paginItem"><a href="javascript:;">'
							+ (currentPage + i) + '</a></li>';
					$(pagenItem).appendTo($(".paginList"));
				}
				pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
				$(pagenItem).prependTo($(".paginList"));
				pagenItem = '<li class="paginItem"><a href="javascript:;">' + 1
						+ '</a></li>';
				$(pagenItem).prependTo($(".paginList"));
				$(prev).prependTo($(".paginList"));

				pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
				$(pagenItem).appendTo($(".paginList"));
				pagenItem = '<li class="paginItem"><a href="javascript:;">'
						+ totalPage + '</a></li>';
				$(pagenItem).appendTo($(".paginList"));
				$(next).appendTo($(".paginList"));
			}
		}
		$(".paginItem").click(function() {
			if ($(this).hasClass("more")) {
				// alert("more");
				return;
			} else if ($(this).find(".pagepre").length > 0) {
				var cur = $('.paginItem.current').find("a").html();
				cur = parseInt(cur);
				if (cur > 1) {
					search(cur - 1);
				}
			} else if ($(this).find(".pagenxt").length > 0) {
				var cur = $('.paginItem.current').find("a").html();
				cur = parseInt(cur);
				if (cur < totalPage) {
					search(cur + 1);
				}
			} else {
				var cur = $(this).find("a").html();
				cur = parseInt(cur);
				search(cur);
			}
		});
	} catch (e) {/* alert(e.messge); */
	}
}

function getSearchParameter(currentPage) {
	try {
		var parameter = $("INPUT[name='parameter']").val();
		var provinceId = $("#searchProvince").val();
		var cityId = $("#searchCity").val();
		var spaceId = $("#searchSpace").val();
		if (provinceId == '0') {
			provinceId = '';
		}
		if (cityId == '0') {
			cityId = '';
		}
		if (spaceId == '0') {
			spaceId = '';
		}

		var visitTimeStart = $("INPUT[name='visitTimeStart']").val();
		var visitTimeEnd = $("INPUT[name='visitTimeEnd']").val();
		var requestJson = {
			currentPage : currentPage,
			data : {
				like : {
					contact : parameter
				},
				equals : {
					'enable' : true,
					'provinceId' : provinceId,
					'cityId' : cityId,
					'spaceId' : spaceId,
					'visitTimeStart' : visitTimeStart,
					'visitTimeEnd' : visitTimeEnd
				}
			}
		};
		return requestJson;
	} catch (e) {
		console.log(e.messge);
	}
	return false;
}

function search(currentPage) {
	var requestJson = getSearchParameter(currentPage);
	if (requestJson == false) {
		return false;
	}
	// console.log(JSON.stringify(requestJson));
	$
			.ajax({
				url : '/euc/clue/query',
				data : JSON.stringify(requestJson),
				beforeSend : function(request) {
					var accessToken = localStorage.getItem('accessToken');
					request.setRequestHeader("accessToken", accessToken);
				},
				dataType : 'JSON',
				async : true,
				contentType : 'application/json;charset=utf-8',
				type : 'POST',
				success : function(result, textStatus, response) {
					// alert(JSON.stringify(result));
					pageBean = result.data;
					$("#userTable").empty();
					$
							.each(
									result.data.list,
									function(i, data) {

										var spaceName = '';
										var spaceId = parseInt(data["spaceId"]);
										if ($.trim(spaceId) != '') {
											spaceName = $
													.trim(spaces[spaceId].spaceName);
										}

										var productTypeName = '';
										if ($.trim(data["productTypeId"]) != '') {
											productTypeName = $
													.trim(productTypes[data["productTypeId"]].productTypeName);
										}

										var sourceOfChannelName = '';
										if ($.trim(data["sourceChannelId"]) != ''
												&& sourceOfChannels[data["sourceChannelId"]] != undefined) {
											sourceOfChannelName = $
													.trim(sourceOfChannels[data["sourceChannelId"]].sourceOfChannelName);
										}

										var processPersonlName = '';
										if (data.processPerson != null) {
											processPersonlName = $
													.trim(data.processPerson.name);
										}

										var bgcolor = localStorage
												.getItem('trcolor');
										if (i % 2 == 0) {
											bgcolor = '';
										}
										var html = '<tr id="t' + data.id + '" '
												+ bgcolor + '>';
										html += '<td>' + (i + 1) + '</td>';
										html += '<td>'
												+ $.trim(data["contact"])
												+ '</td>';
										html += '<td>'
												+ cities[data.cityId].cityName
												+ '</td>';
										html += '<td>' + spaceName + '</td>';
										html += '<td>' + productTypeName
												+ '</td>';
										html += '<td>' + sourceOfChannelName
												+ '</td>';
										html += '<td>' + processPersonlName
												+ '</td>';
										html += '<td>' + $.trim(data.visitTime)
												+ '</td>';
										html += '<td>' + data.createTime
												+ '</td>';
										html += '<td style="width:170px;"><a href="javascript:detail('
												+ data.id
												+ ');" class="tablelink">编辑</a>&nbsp;&nbsp;&nbsp;<a href="javascript:deleteData('
												+ data.id
												+ ');" class="tablelink">删除</a></td>';
										html += '</tr>';
										$("#userTable").append(html);
										dataList[data.id] = data;
									});
					$("#totalRow").html(pageBean.totalRow + '&nbsp;');
					$("#currentPage").html(pageBean.currentPage + '&nbsp;');
					$("#pageSize").html(pageBean.pageSize + '&nbsp;');
					var pageLength = 15;
					initailPageNumber(pageBean.totalPage, pageBean.currentPage,
							pageLength);
				},
				error : function() {
					console.log("error");
				}
			});
}
